package com.hopedove.callsystem.activity.call;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.hopedove.base.BaseActivity;
import com.hopedove.callsystem.R;
import com.hopedove.callsystem.databinding.ActiviyConfirmcallBinding;
import com.hopedove.callsystem.domain.net.FinishCall;
import com.hopedove.net.ErrResponse;
import com.hopedove.net.OkResponse;
import com.hopedove.net.domain.Communication;
import com.hopedove.utils.ToastUtil;
import com.hopedove.view.TipsDialogFragment;


public class ConfirmCallActivity extends BaseActivity implements OnClickListener {

    private TipsDialogFragment tipDialog;
    private ActiviyConfirmcallBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActiviyConfirmcallBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.confirm.setOnClickListener(this);

        // 控制输入框只能接受11为数字，超过11位后，自动覆盖
        binding.machineNo.addTextChangedListener(new TextWatcher() {
            private int editEnd;
            private CharSequence temp;

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                temp = s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                editEnd = binding.machineNo.getSelectionEnd();
                if (temp.length() > 11) {
                    s.delete(0, editEnd - 1);
                    binding.machineNo.setText(s);
                    binding.machineNo.setSelection(editEnd + 1);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.confirm) {
            final String no = binding.machineNo.getText().toString();
            if (TextUtils.isEmpty(no)) {
                ToastUtil.longToast(this, "请扫描条码");
                return;
            }

            View view = getLayoutInflater().inflate(R.layout.loading, null);
            TextView textView = view.findViewById(R.id.tip);
            String tipStr = "呼叫中...";
            textView.setText(tipStr);
            tipDialog = TipsDialogFragment.showNewInstance(this, view, false);

            Communication.asyncRequest(new FinishCall(no), r -> {
                TipsDialogFragment.dismissTipDialog(tipDialog);

                if (r instanceof OkResponse) {
                    tipDialog = TipsDialogFragment.showNewInstance(this, R.layout.callsys_tips_type_1, false);
                    uiHandler.postDelayed(() -> {
                        TipsDialogFragment.dismissTipDialog(tipDialog);
                        Intent intent = new Intent(getBaseContext(), CallMainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    }, 3000);
                } else if (r instanceof ErrResponse) {
                    ToastUtil.longToast(getBaseContext(), ((ErrResponse) r).getErrText());
                }
            });
        }
    }

}
